﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using UI.Grid.PersonSvcProxy;

namespace UI.Grid
{
    public partial class EntityGridView : UserControl
    {
        private List<Entity> _Entities;

        public EntityGridView()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.PopulatePersons();
        }

        private void PopulatePersons()
        {
            PersonServiceClient client = new PersonServiceClient();
            client.GetPersonsCompleted += new EventHandler<GetPersonsCompletedEventArgs>(client_GetPersonsCompleted);
            client.GetPersonsAsync(new SearchKey());
            client.CloseAsync();
        }

        void client_GetPersonsCompleted(object sender, GetPersonsCompletedEventArgs e)
        {
            List<Person> persons = e.Result;
            this._Entities=new List<Entity>();
            if(persons !=null && persons.Count>0)
            {
                foreach(Person person in persons)
                {
                    this._Entities.Add(person);
                }
            }
            this.DataBind();
        }

        public void naviControl_OnPageIndexChanged(object sender, int pageIndex)
        {
            this._CurrentPageIndex = pageIndex;
            this.DataBind();
        }

        #region props
        private bool _EnablePaging = true;
        /// <summary></summary>
        public bool EnablePaging
        {
            get
            {
                return this._EnablePaging;
            }
            set
            {
                this._EnablePaging = value;
            }
        }

        private bool _ShowTopPagingNavi=true;
        /// <summary></summary>
        public bool ShowTopPagingNavi
        {
            get
            {
                return this._ShowTopPagingNavi;
            }
            set
            {
                this._ShowTopPagingNavi = value;
            }
        }

        private bool _ShowBottomPagingNavi=true;
        /// <summary></summary>
        public bool ShowBottomPagingNavi
        {
            get
            {
                return this._ShowBottomPagingNavi;
            }
            set
            {
                this._ShowBottomPagingNavi = value;
            }
        }

        /// <summary></summary>
        public int TotalRecords
        {
            get
            {
                if (this._Entities != null)
                    return this._Entities.Count;
                else
                    return 0;
            }
        }

        private int _PageSize = 20;
        /// <summary></summary>
        public int PageSize
        {
            get
            {
                return this._PageSize;
            }
            set
            {
                this._PageSize = value;
            }
        }

        private int _CurrentPageIndex;
        /// <summary></summary>
        public int CurrentPageIndex
        {
            get
            {
                return this._CurrentPageIndex;
            }
            set
            {
                this._CurrentPageIndex = value;
            }
        }
        #endregion

        #region data bind
        protected void DataBind()
        {
            if (this._Entities != null && this._Entities.Count > 0)
            {
                if(this._EnablePaging)
                {
                    if(this.ShowTopPagingNavi)
                    {
                        this.naviControl1.Visibility = Visibility.Visible;
                        this.naviControl1.AttachedGrid = this;
                        this.naviControl1.Current = this._CurrentPageIndex;
                    }
                    else
                    {
                        this.naviControl1.Visibility = Visibility.Collapsed;
                    }
                    if(this.ShowBottomPagingNavi)
                    {
                        this.naviControl2.Visibility = Visibility.Visible;
                        this.naviControl2.AttachedGrid = this;
                        this.naviControl2.Current = this._CurrentPageIndex;
                    }
                    else
                    {
                        this.naviControl2.Visibility = Visibility.Collapsed;
                    }
                    List<object> copy = new List<object>();
                    int startIndex = this._PageSize*this._CurrentPageIndex;
                    int endIndex = startIndex + this._PageSize;
                    for(int i=startIndex; i<endIndex;i++)
                    {
                        if (i >= 0 && i < this._Entities.Count)
                            copy.Add(this._Entities[i]);
                    }
                    this.dgOrgs.ItemsSource = copy;
                }
                else
                {
                    this.naviControl1.Visibility = Visibility.Collapsed;
                    this.naviControl2.Visibility = Visibility.Collapsed;
                    this.dgOrgs.ItemsSource = this._Entities;
                }
            }
        }

        public void BindEntities(List<Entity> entities)
        {
            this._Entities = entities;
            this.DataBind();
        }
        #endregion
    }
}
